//
//  MahjongGameTVC.m
//  Mahjong
//
//  Created by 饶首建 on 17/6/29.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "MahjongGameTVC.h"

#import "ActionModel.h"
#import "ControlButton.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface MahjongGameTVC()

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *tagContainerScrollView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *seperatorView;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;

@end

@implementation MahjongGameTVC

- (void)setResultModel:(ResultModel *)resultModel{
    
    _headImageView.layer.cornerRadius = _headImageView.frame.size.width/2;
    _headImageView.layer.masksToBounds = YES;
    
    //头像
    if (resultModel.playerModel.avatar.length) {
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:resultModel.playerModel.avatar] placeholderImage:UIImageWithName(@"default_head")];
        _firstLabel.text = @"";
    }else{
        _firstLabel.text = [resultModel.playerModel.playerName substringToIndex:1];
        _headImageView.image = UIImageWithName(@"default_head");
    }

    _seperatorView.hidden = _notShowLine;
    
    _resultModel = resultModel;
    _nicknameLabel.text = resultModel.playerModel.playerName;
    
    
    if ([resultModel.playerModel.playerId isEqual:_owner.playerId]) {
        _nicknameLabel.textColor = UIColorHex(66B5F0);
    }else{
        _nicknameLabel.textColor = UIColorHex(4A4A4A);
    }
    
    if (_resultModel.score > 0) {
        _scoreLabel.textColor = UIColorHex(F15A5A);
        _scoreLabel.text = NSStringFormat(@"+%ld",resultModel.score);
    }else if (_resultModel.score == 0){
        _scoreLabel.textColor = UIColorHex(9B9B9B);
        _scoreLabel.text = @"0";
    }else{
        _scoreLabel.textColor = UIColorHex(519B00);
        _scoreLabel.text = NSStringFormat(@"%ld",resultModel.score);
    }
    
    NSMutableArray *attrDisplayArr = [NSMutableArray new];
    NSMutableArray *attrDisplayActionId = [NSMutableArray new];
    for (ActionModel *am in _gameModel.actionArr) {
        if ([am.owner.playerId isEqualToString:resultModel.playerModel.playerId]) {
            [attrDisplayArr addObject:am.displayName];
            [attrDisplayActionId addObject:am.actionId];
        }
    }
    
    for (UIView *v in _tagContainerScrollView.subviews) {
        [v removeFromSuperview];
    }
    
    float x = 0;
    float y = _tagContainerScrollView.frame.size.height;
    
    for (int i=0;i<attrDisplayArr.count;i++) {
        NSString *title = attrDisplayArr[i];

        ControlButton *btn = [[ControlButton alloc]initWithTitle:title fontSize:24];

        CGRect frame = btn.frame;
        frame.size.width = frame.size.width + 20;
        frame.origin.y = 20;
        frame.origin.x = CGRectGetMaxX(_tagContainerScrollView.subviews.lastObject.frame)+5;
        frame.size.height = y - 40;
        btn.frame = frame;
        x += 5 + frame.size.width;
        
        //标签点击事件:只能改自己的
        if ([resultModel.playerModel.playerId isEqual:_owner.playerId]) {
            btn.layer.borderColor = UIColorHex(F6A623).CGColor;
            [btn setTitleColor:UIColorHex(F6A623) forState:UIControlStateNormal];
            btn.backgroundColor = UIColorFromRGBA(0xF6A623, 0.1);
            
            [btn addTapBlock:^(UIButton *btn) {
                for (ActionModel *am in _gameModel.actionArr) {
                    if ([am.actionId isEqualToString:attrDisplayActionId[i]]) {
                        if (_tagClickBlock) {
                            _tagClickBlock(am);
                        }
                        break;
                    }
                }
            }];
        }else{
            btn.layer.borderColor = UIColorHex(9b9b9b).CGColor;
            [btn setTitleColor:UIColorHex(9b9b9b) forState:UIControlStateNormal];
            btn.backgroundColor = UIColorFromRGBA(0x9b9b9b, 0.1);
        }
        
        [_tagContainerScrollView addSubview:btn];
    }
    [_tagContainerScrollView setContentSize:CGSizeMake(x, y)];
    if (x > _tagContainerScrollView.frame.size.width) {
        [_tagContainerScrollView scrollToRight];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
